#pragma once

#include "socket_types.h"
#include "inet_address.h"

#include <functional>
#include <vector>

class InetAddress;
class Socket;
class ServerSocket {
public:
    ServerSocket(const InetAddress& bindAddr, int backlog = 50);
    void bind(const InetAddress& bindAddr, int backlog = 50);
    void startListening();
    Socket* accept();
    void close();
    bool isClosed();
private:
    bool closed_ = true;
    socket_type sockfd_ = invalid_socket;
    int backlog_ = 50;
    std::vector<Socket*> clientSockets_;
    void workThread_();
public:
    /* callback function */
    std::function<void(Socket*)> onAccept;
    std::function<void(Socket*, const std::vector<char>&)> onMessage;
    std::function<void(Socket* socket)> onClose;
};

